﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct goods {
    char name;
    int channel, amount, price;
};

int main() {

    int time = 3;

    //开始加入
    struct goods a;
    printf("添加货物，请输入：名称 通道 数量 价格\n");

    while (scanf("%c %d %d %d", &a.name, &a.channel, &a.amount, &a.price) != 4)
    {
        while (getchar() != '\n');
        printf("错误，重新添加货物，请输入：名称 通道 数量 价格\n");
    }
    while (getchar() != '\n');
    printf("你的输入是：%c %d %d %d\n", a.name, a.channel, a.amount, a.price);

    //开始选择购买货物
    a:printf("进入选择界面\n");
    char my_name;
    int my_channel, my_amount;
    printf("请输入购买的货物：名称 通道 数量\n");
    while (scanf("%c %d %d", &my_name, &my_channel, &my_amount) != 3 || my_name != a.name || my_channel != a.channel || my_amount > a.amount) {
        while (getchar() != '\n');
        if (my_name != a.name) {
            printf("货物不存在,请再次输入\n");
        }
        if (my_channel != a.channel) {
            printf("通道错误\n");
        }
        if (my_amount > a.amount) {
            printf("货物数量不足");
        }
    }
    while (getchar() != '\n');

    

    //开始付款
   b: printf("进入付款界面，请确认\n");
    int total_money = a.price * my_amount;
    printf("你购买的是:%c,数量为:%d,应付：%d\n", my_name, my_amount, total_money);

    //回退操作
    char bet[] = "back";
    char ret[100];
    

    printf("回退或继续,回退请输入back并回车，继续请输入任意字符并回车");
    //printf("%s\n", bet);

    scanf("%s99", ret);

    if (strcmp(bet, ret) == 0 && time > 0)
    {
        time--;
        printf("您选择撤回一次操作,还有%d次机会", time);
        goto a;
    }

    else
    {
        printf("程序继续");
    }
    //完成回退
   c: printf("请开始付款\n");
   
    int input_money = 0;

    while (total_money > 0) {
        scanf("%d", &input_money);
        while (input_money!= 1&&input_money!=2&&input_money!=5) {
            // 清除输入缓冲区
            while (getchar() != '\n');

            printf("输入错误，请塞入1,2,5\n已退钱,请重新付款：");
            goto c;
        }

        total_money -= input_money;
        printf("你塞了 %d 元，还剩 %d 元\n", input_money, total_money);
    }
    //开始找零
    printf("找零 %d 元", -total_money);

    //回退操作


        char get[] = "back";
        char set[100];
        

        printf("回退或继续,回退请输入back并回车，继续请输入任意字符并回车");
            //printf("%s\n", bet);

            scanf("%s99", set);

        if (strcmp(get, set) == 0 && time > 0)
        {
            time--;
            printf("您选择撤回一次操作,还有%d次机会", time);
            goto b;
        }

        else
        {
            printf("程序继续");
        }
    //完成回退

        return 0;
}